﻿@using Infragistics.Web.Mvc

@model IQueryable<UscSunShine.Mvc.Models.PermissionViewModel>

@section styles
{
    @Styles.Render("~/IgniteUI/css")
}


@{
    ViewBag.Title = "Create";
}

<h2>角色授权</h2>

<div class="form-horizontal">
    <div class="form-group">
        @Html.Label("角色", new { @class = "col-md-2 control-label" })
        <div class="col-md-4">
            @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList, new { @disabled = "disabled", @class = "form-control" })
        </div>
        <input type="button" value="保存" onclick=" getRowsInfo(); " class="btn btn-default" />
    </div>
    <div class="form-group">
        @(Html.Infragistics()
                  .Grid(Model)
                  .ID("Grid")
                  .Height("450px")
                  .Width("100%")
                  .AutoGenerateColumns(false)
                  .AutoGenerateLayouts(false)
                  .RenderCheckboxes(true)
                  .PrimaryKey("Id")
                  .Columns(column =>
                  {
                      column.For(x => x.Id).Hidden(true);
                      column.For(x => x.Action).HeaderText("Action名称");
                      column.For(x => x.Controller).HeaderText("Controller名称");
                      column.For(x => x.Description).HeaderText("功能说明");
                  })
                  .Features(feature =>
                  {
                      feature.Selection().Mode(SelectionMode.Row).MultipleSelection(true);
                      feature.RowSelectors().EnableRowNumbering(true).EnableCheckBoxes(true);
                      feature.Sorting();
                      feature.Paging().PageSize(10)
                          .FirstPageLabelText("第一页")
                          .LastPageLabelText("最后一页")
                          .NextPageLabelText("下一页")
                          .PageSizeDropDownLabel("每页记录数")
                          .PrevPageLabelText("前一页");
                  })
                //.DataSourceUrl(Url.Action("GetPermissions"))
                  .DataBind()
                  .Render()
        )
    </div>
</div>


<div>
    @Html.ActionLink("返回角色权限列表", "Index", new { roleId = ((SelectList)ViewBag.RoleID).SelectedValue })
</div>

@section scripts
{
    <script>
        function getRowsInfo() {
            var selectedRows = $("#Grid").igGridSelection("selectedRows"), data = [], cellVal;
            if (selectedRows.length == 0) {
                alert("请选择记录");
                return false;
            }
            //取roleID
            var roleId = $("#RoleID").val();
            //取token
            var token = $("input[name='__RequestVerificationToken']").val();
            //取列数据
            var gridColumns = $("#Grid").igGrid("option", "columns");
            for (var j = 0; j < selectedRows.length; j++) {
                var row = selectedRows[j];
                var rowData = {};
                //取ID值
                cellVal = $("#Grid").igGrid("getCellValue", row.id, "Id");
                rowData["Id"] = cellVal;
                data[j] = rowData;
            }
            //提交服务端保存
            $.post("/RolePermissions/Create",
                {
                    "__RequestVerificationToken": token,
                    "roleId": roleId,
                    "data": data
                },
                function (result) {
                    if (result.Success) {
                        //跳转到Index
                        window.location = "Index?roleId=" + roleId;
                    } else {
                        //刷新当前
                        location.reload();
                    }
                });
        }
    </script>

    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/IgniteUI/js")
}